<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        :option="option"
        :table-loading="loading"
        @refresh-change="initList"
        @search-change="searchChange"
        :page.sync="page"
        @row-save="rowSave"
        @row-update="rowUpdate"
        @row-del="rowDel"
        v-model="from"
        :before-open="beforeOpen"
      >
        <template slot="addressForm">
          <el-cascader
            v-model="addressValue"
            :options="options"
            @change="handleChange"
          ></el-cascader>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
  import {option} from "@/const/crud/houxin/company";
  import {addObj, delObj, fetchList, queryRegionList, updateObj,} from "@/api/houxin/company";

  export default {
    data() {
      return {
        options: [],
        addressValue: "",
        from: {},
        option: option,
        data: [],
        loading: true,
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条
        },
      };
    },
    created() {
      queryRegionList().then((r) => {
        this.options = this.handleTree(r.data.data);
      });
      this.listdata();
    },
    methods: {
      beforeOpen(done, type) {
        if (type === "edit") {
          this.addressValue = this.from.address.split("/");
        }
        done();
      },
      handleTree(data) {
        let array = [];
        data.forEach((e) => {
          let item = {};
          item.value = e.name;
          item.label = e.name;
          if (e.subRegionList) {
            item.children = this.handleTree(e.subRegionList);
          }
          array.push(item);
        });
        return array;
      },
      handleChange() {
        this.from.address = this.addressValue.join("/");
      },
      initList() {
        this.listdata();
      },
      listdata(params = {pageIndex: this.page.currentPage, pageSize: this.page.pageSize}) {
        this.loading = true;
        fetchList(params).then((r) => {
          this.data = r.data.data.list;
          this.page.total = r.data.data.total;
          this.loading = false;
        });
      },
      searchChange(form, done) {
        this.listdata({
          pageIndex: this.page.currentPage,
          pageSize: this.page.pageSize,
          ...form,
        });
        done();
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      rowEdit(row) {
        this.$refs["ChannelForm"].edit(row);
      },
      rowSave(form, done, loading) {
        addObj(form).then((r) => {
          this.listdata();
        });

        loading();
        done();
      },
      rowUpdate(form, index, done, loading) {
        updateObj(form).then((r) => {
          this.listdata();
        });
        loading();
        done();
      },
      rowDel(row, index) {
        this.$confirm("删除操作, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            delObj({enterprisesIds: [row.id]}).then((r) => {
              this.listdata();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            });
          })
          .catch(() => {
          });
      },
    },
  };
</script>

<style>
</style>
